package com.ruoyi.project.repairs.controller;

import java.util.List;

import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.project.system.domain.SysRole;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.repairs.domain.LblCsMaintainInfo;
import com.ruoyi.project.repairs.service.ILblCsMaintainInfoService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 维修信息Controller
 * 
 * @author BaoL
 * @date 2023-03-25
 */
@RestController
@RequestMapping("/repairs/repairs")
public class LblCsMaintainInfoController extends BaseController
{
    @Autowired
    private ILblCsMaintainInfoService lblCsMaintainInfoService;
    @Autowired
    private ISysUserService userService;

    @Autowired
    private TokenService tokenService;
    /**
     * 查询维修信息列表
     */
    @PreAuthorize("@ss.hasPermi('repairs:repairs:list')")
    @GetMapping("/list")
    public TableDataInfo list(LblCsMaintainInfo lblCsMaintainInfo)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser user = loginUser.getUser();
        List<SysRole> roles = user.getRoles();
        for (SysRole role : roles) {
            // 若是员工角色, 则直接查询被分配到的任务
            if (role.getRoleName().indexOf("维修员工") != -1){
                lblCsMaintainInfo.setSysUserEmpId(user.getUserId().intValue());
                break;
            }
        }
        startPage();
        List<LblCsMaintainInfo> list = lblCsMaintainInfoService.selectLblCsMaintainInfoList(lblCsMaintainInfo);
        return getDataTable(list);
    }

    /**
     * 导出维修信息列表
     */
    @PreAuthorize("@ss.hasPermi('repairs:repairs:export')")
    @Log(title = "维修信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LblCsMaintainInfo lblCsMaintainInfo)
    {
        List<LblCsMaintainInfo> list = lblCsMaintainInfoService.selectLblCsMaintainInfoList(lblCsMaintainInfo);
        ExcelUtil<LblCsMaintainInfo> util = new ExcelUtil<LblCsMaintainInfo>(LblCsMaintainInfo.class);
        return util.exportExcel(list, "repairs");
    }

    /**
     * 获取维修信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('repairs:repairs:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(lblCsMaintainInfoService.selectLblCsMaintainInfoById(id));
    }

    /**
     * 新增维修信息
     */
    @PreAuthorize("@ss.hasPermi('repairs:repairs:add')")
    @Log(title = "维修信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LblCsMaintainInfo lblCsMaintainInfo)
    {
        return toAjax(lblCsMaintainInfoService.insertLblCsMaintainInfo(lblCsMaintainInfo));
    }

    /**
     * 修改维修信息
     */
    @PreAuthorize("@ss.hasPermi('repairs:repairs:edit')")
    @Log(title = "维修信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LblCsMaintainInfo lblCsMaintainInfo)
    {
        return toAjax(lblCsMaintainInfoService.updateLblCsMaintainInfo(lblCsMaintainInfo));
    }

    /**
     * 删除维修信息
     */
    @PreAuthorize("@ss.hasPermi('repairs:repairs:remove')")
    @Log(title = "维修信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(lblCsMaintainInfoService.deleteLblCsMaintainInfoByIds(ids));
    }

    /**
     * 查询该角色下的用户列表
     */
    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/userByRoleIdList")
    public TableDataInfo userByRoleIdList(SysUser user)
    {
        startPage();
        List<SysUser> list = userService.selectUserByRoleId(user);
        return getDataTable(list);
    }
}
